package com.poobo.Interceptor;

// add by hxh 20160517
import java.io.File;
import java.io.FileWriter;
import java.io.FileOutputStream;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.poobo.constants.WebConstants;
import com.poobo.entity.TbSysAccount;


public class ActionInterceptor implements HandlerInterceptor {

	private Logger logger= Logger.getLogger(ActionInterceptor.class);
	private static final String[] IGNORE_URI = {"/login.html", "/chLoginCode.html","/toLoginOut.html","login/","/getCode.html","/updateCode.html",
		"/findEquipmentConfingList.html","/findEquipmentDefaultGame.html","/findByEquipmentNumber.html","/findEquipStateByEquipmentNumber.html","/generateXmlByconfigListId.html"};
	
	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object object, Exception exception)
			throws Exception {
		
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response,
			Object object, ModelAndView view) throws Exception {
		
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object object) throws Exception {
		 boolean flag = false;
	        String url = request.getRequestURL().toString();
	        for (String s : IGNORE_URI) {
	            if (url.contains(s)) {
	                flag = true;
	                break;
	            }
	        }
	        if (!flag) {
	            TbSysAccount user = (TbSysAccount) request.getSession().getAttribute(WebConstants.CURRENT_USER);
	            if (user != null) {
	            	flag = true;
	            }else{
	            	response.sendRedirect("login.html");
	            }
	        }
	        
	        return flag;
		   
	}
	
	// add by hxh 20160517
	public static void generateXml(String filename){
	       // 建立document对象 
	       Document document = DocumentHelper.createDocument();

	       // 创建根节点
	       Element root = DocumentHelper.createElement("AllConfigureRecord");
	       document.setRootElement(root);
	       
	       //Element root = document.addElement("AllConfigureRecord");
	       // 加入一行注释 
	       //root.addComment("This is a test for dom4j, holen, 2004.9.11");
	       
	       // 加入属性内容
	       root.addAttribute("Id","92");
	       root.addAttribute("name","出厂配置（驯龙广告片）");
	       root.addAttribute("isEnable","1");
	       
	       for(int i=0;i<5;i++){
		       // 加入第i个节点 
		       Element node = root.addElement("ConfigureRecord");
		       // 加入属性内容
		       node.addAttribute("resourceId","null");
		       node.addAttribute("resourceType","0");
		       node.addAttribute("appName","null");
		       node.addAttribute("URL","null");
		       node.addAttribute("version","null");
		       node.addAttribute("startTime","2015-11-11 06:00:00");
		       node.addAttribute("stopTime","2015-11-11 11:00:00");
		       node.addAttribute("orderNumber","1");
		       node.addAttribute("weekday","2");
		       node.addAttribute("showTime","0");
	       }

	       try{
	           // 将document中的内容写入文件中
	    	   OutputFormat format = OutputFormat.createPrettyPrint();
	    	   format.setEncoding("UTF-8");
	           XMLWriter writer = new XMLWriter(new FileOutputStream(filename),format);
	           writer.write(document);
	           writer.close();
	       }catch(Exception ex){
	           ex.printStackTrace();
	       }
	       
	}
	
	
}
